package com.zhiwang.planet.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.zhiwang.planet.R;


/**
 * @author: CJJ
 * @date 2017/4/17
 */
public class RoundShapeTextView extends AppCompatTextView {


    private static final String TAG = "TextDrawable";
    private boolean roundCorner;
    private float corner;
    private Paint paint;
    private boolean autoCorner;
    private Path path;
    private int backgroundColor;

    public RoundShapeTextView(Context context) {
        super(context);
    }

    public RoundShapeTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundShapeTextView);
        corner = (int) array.getDimension(R.styleable.RoundShapeTextView_corner, 0);
        autoCorner = array.getBoolean(R.styleable.RoundShapeTextView_auto_corner, false);
        backgroundColor = array.getColor(R.styleable.RoundShapeTextView_background_color, Color.parseColor("#cccccc"));
        roundCorner = array.getBoolean(R.styleable.RoundShapeTextView_round_corner, false);
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(backgroundColor);
        paint.setAntiAlias(true);
        array.recycle();
    }

    public RoundShapeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setBackgroundColor(@ColorInt int color) {
        backgroundColor = color;
        paint.setColor(backgroundColor);
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

//        doAutoCorner = autoCorner;
        if (autoCorner) {
            Paint textPaint = getPaint();
            int textLen = (int) textPaint.measureText(getText(), 0, getText().length());
            Paint.FontMetrics fm = textPaint.getFontMetrics();
            int textHeight = (int) (-fm.top + fm.descent);
            int max = Math.max(textLen, textHeight);
            int d = (int) (max * 1.414);
            int p = d / 2 - max / 2;
            int extraPadding = 10;
            int topBottomPadding = (max - textHeight) / 2 + extraPadding;
            int leftRightPadding = (max - textLen) / 2 + extraPadding;
            setPadding(leftRightPadding, topBottomPadding, leftRightPadding, topBottomPadding);
            setMeasuredDimension(max + extraPadding * 2, max + extraPadding * 2);
            corner = d * 1.5f;
        } else super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        float left = 0;
        float top = 0;
        float right = getMeasuredWidth();
        float bottom = getMeasuredHeight();
        if (!autoCorner) {
            float radius = roundCorner ? getMeasuredHeight() / 2f : corner;
            canvas.drawRoundRect(new RectF(0f, 0f, (float) getMeasuredWidth(), (float) getMeasuredHeight()), radius, radius, paint);
        } else {
            path = CirclePath(left, top, right, bottom, corner);
            canvas.drawCircle(getMeasuredWidth() / 2f, getMeasuredHeight() / 2f, Math.min(getMeasuredWidth() / 2f, getMeasuredHeight() / 2f), paint);
        }
        super.onDraw(canvas);
    }

    private Path CirclePath(float left, float top, float right, float bottom, float corner) {
        Path path = new Path();
        RectF rectF = new RectF(left, top, right, bottom);
        path.addArc(rectF, 0, 360);
        return path;
    }

    public Path RoundedRect(float left, float top, float right, float bottom, float rx, float ry,
                            boolean tl, boolean tr, boolean br, boolean bl) {
        Path path = new Path();
        if (rx < 0) {
            rx = 0;
        }
        if (ry < 0) {
            ry = 0;
        }
        float width = right - left;
        float height = bottom - top;
        if (rx > width / 2) {
            rx = width / 2;
        }
        if (ry > height / 2) {
            ry = height / 2;
        }
        float widthMinusCorners = (width - (2 * rx));
        float heightMinusCorners = (height - (2 * ry));

        path.moveTo(right, top + ry);
        if (tr)
            path.rQuadTo(0, -ry, -rx, -ry);//top-right corner
        else {
            path.rLineTo(0, -ry);
            path.rLineTo(-rx, 0);
        }
        path.rLineTo(-widthMinusCorners, 0);
        if (tl)
            path.rQuadTo(-rx, 0, -rx, ry); //top-left corner
        else {
            path.rLineTo(-rx, 0);
            path.rLineTo(0, ry);
        }
        path.rLineTo(0, heightMinusCorners);

        if (bl)
            path.rQuadTo(0, ry, rx, ry);//bottom-left corner
        else {
            path.rLineTo(0, ry);
            path.rLineTo(rx, 0);
        }

        path.rLineTo(widthMinusCorners, 0);
        if (br)
            path.rQuadTo(rx, 0, rx, -ry); //bottom-right corner
        else {
            path.rLineTo(rx, 0);
            path.rLineTo(0, -ry);
        }

        path.rLineTo(0, -heightMinusCorners);
        path.close();//Given close, last lineto can be removed.

        return path;
    }
}
